package com.bstek.xxl.job.admin.controller.interceptor;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.xxl.job.admin.controller.annotation.PermissionLimit;
import com.bstek.xxl.job.admin.core.model.XxlJobUser;
import com.bstek.xxl.job.admin.core.util.CookieUtil;
import com.bstek.xxl.job.admin.core.util.FtlUtil;
import com.bstek.xxl.job.admin.core.util.I18nUtil;
import com.bstek.xxl.job.admin.service.LoginService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限拦截
 *
 * @author xuxueli 2015-12-12 18:09:04
 */
@Component
public class PermissionInterceptor implements AsyncHandlerInterceptor {

	@Resource
	private LoginService loginService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//		set(request,response);
		if (!(handler instanceof HandlerMethod)) {
			return true;	// proceed with the next interceptor
		}
		// if need login
		boolean needLogin = true;
		boolean needAdminuser = false;
		HandlerMethod method = (HandlerMethod)handler;
		PermissionLimit permission = method.getMethodAnnotation(PermissionLimit.class);
		if (permission!=null) {
			needLogin = permission.limit();
			needAdminuser = permission.adminuser();
		}

		if (needLogin) {
			XxlJobUser loginUser = loginService.ifLogin(request, response);
			if (loginUser == null) {
				response.setStatus(302);
				response.setHeader("location", request.getContextPath()+"/toLogin");
				return false;
			}
			if (needAdminuser && loginUser.getRole()!=1) {
				throw new RuntimeException(I18nUtil.getString("system_permission_limit"));
			}
			request.setAttribute(LoginService.LOGIN_IDENTITY_KEY, loginUser);
		}

		return true;	// proceed with the next interceptor
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		System.out.println("modelAndView.............");
		modelAndView.addObject("I18nUtil", FtlUtil.generateStaticModel(I18nUtil.class.getName()));


	}

	private void set(HttpServletRequest request, HttpServletResponse response) {

		IUser user= ContextHolder.getLoginUser();
		if(user!=null){

		}
		XxlJobUser xxlJobUser1 = loginService.ifLogin(request, response);
		if(xxlJobUser1==null){

		}

		Object attribute = request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
		System.out.println("LOGIN_IDENTITY_KEY"+attribute);
		XxlJobUser loginUsers = loginService.ifLogin(request, response);
		if(loginUsers!=null){
			request.setAttribute(LoginService.LOGIN_IDENTITY_KEY, loginUsers);

		}
//			  String username = user.getUsername();
//			  XxlJobUser xxlJobUser = xxlJobUserDao.loadByUserName(username);
//
//			  request.setAttribute(LoginService.LOGIN_IDENTITY_KEY, loginUser);




	}



}
